//package com.yunhe.device.service.device.impl;
//
//import com.yunhe.common.util.StringUtil;
//import com.yunhe.device.client.AuthorityClient;
//import com.yunhe.common.model.PageParam;
//import com.yunhe.device.domain.device.EnergyUnit;
//import com.yunhe.common.exception.ArgumentErrorException;
//import com.yunhe.common.exception.ObjectNotFoundException;
//import com.yunhe.common.exception.UniqueConstraintsException;
//import com.yunhe.common.model.authority.ResourceTree;
//import com.yunhe.device.repository.device.EnergyUnitRepository;
//import com.yunhe.device.service.device.EnergyUnitService;
//import com.yunhe.common.util.QueryUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Sort;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Collections;
//import java.util.Date;
//import java.util.List;
//import java.util.Optional;
//
///**
// * 能量单元service
// * @desc 能量单元服务类实现
// * @author liuronglei
// */
//@Service
//public class EnergyUnitServiceImpl implements EnergyUnitService {
//
//    @Autowired
//    private EnergyUnitRepository energyUnitRepository;
//    @Autowired
//    private AuthorityClient authorityClient;
//
//    private void processBeforeSave(EnergyUnit energyUnit)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        if (energyUnit.getCode() == null
//                || energyUnit.getName() == null
//                || energyUnit.getTitle() == null) {
//            throw new ArgumentErrorException("参数不能为空");
//        }
//        String name = energyUnit.getName();
//        EnergyUnit unitByName = energyUnitRepository.findByName(name);
//        if (unitByName != null) {
//            if (energyUnit.getId() == null || unitByName.getId().longValue() != energyUnit.getId()) {
//                throw new UniqueConstraintsException("已存在名称为" + name + "的能量单元");
//            }
//        }
//        Long code = energyUnit.getCode();
//        EnergyUnit unitByCode = energyUnitRepository.findByCode(code);
//        if (unitByCode != null) {
//            if (energyUnit.getId() == null || unitByCode.getId().longValue() != energyUnit.getId()) {
//                throw new UniqueConstraintsException("已存在代码为" + code + "的能量单元");
//            }
//        }
//    }
//
//    @Override
//    @Transactional
//    public EnergyUnit createEnergyUnit(Long firmId,
//                                       Long stationId,
//                                       EnergyUnit energyUnit)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        energyUnit.setId(null);
//        processBeforeSave(energyUnit);
//        EnergyUnit result = energyUnitRepository.save(energyUnit);
//        if (result != null) {
//            //绑定单位
//            authorityClient.firmBindResource(firmId, "EnergyUnit", result.getId());
//            //绑定电站
//            authorityClient.stationBindResource(stationId, "EnergyUnit", Collections.singletonList(result.getId()));
//        }
//        return result;
//    }
//
//    @Override
//    @Transactional
//    public void deleteById(Long id) {
//        energyUnitRepository.deleteById(id);
//    }
//
//    @Override
//    @Transactional
//    public EnergyUnit updateEnergyUnit(EnergyUnit energyUnit)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        processBeforeSave(energyUnit);
//        return energyUnitRepository.save(energyUnit);
//    }
//
//    @Override
//    @Transactional
//    public EnergyUnit updateEnergyUnit(Long id,
//                                       Long code,
//                                       String name,
//                                       String title,
//                                       Double capacity,
//                                       Date productionTime)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        Optional<EnergyUnit> energyUnitOpt = energyUnitRepository.findById(id);
//        if (!energyUnitOpt.isPresent()) {
//            throw new ObjectNotFoundException("找不到ID为" + id + "的能量单元");
//        }
//        EnergyUnit energyUnit = energyUnitOpt.get();
//        Optional.ofNullable(code).ifPresent(energyUnit::setCode);
//        Optional.ofNullable(name).ifPresent(energyUnit::setName);
//        Optional.ofNullable(title).ifPresent(energyUnit::setTitle);
//        Optional.ofNullable(capacity).ifPresent((energyUnit::setCapacity));
//        Optional.ofNullable(productionTime).ifPresent(energyUnit::setProductionTime);
//        processBeforeSave(energyUnit);
//        return energyUnitRepository.save(energyUnit);
//    }
//
//    /**
//     * 根据能量单元ID获得能量单元
//     * @param id 能量单元ID
//     */
//    @Override
//    public EnergyUnit findById(Long id) {
//        return energyUnitRepository.findById(id).orElse(null);
//    }
//
//    /**
//     * 根据能量单元代码获得能量单元
//     * @param code 能量单元代码
//     */
//    @Override
//    public EnergyUnit findByCode(Long code) {
//        return energyUnitRepository.findByCode(code);
//    }
//
//    @Override
//    public Iterable<EnergyUnit> search(Long stationId,
//                                       Long stationCode,
//                                       PageParam pageParam) {
//        Iterable<EnergyUnit> results;
//        Long stationIdVal = Optional.ofNullable(stationId).orElse((getIdByCode(stationCode)));
//        if (stationIdVal != null) {
//            if (QueryUtil.needPaging(pageParam)) {
//                int page = QueryUtil.getPage(pageParam);
//                int size = QueryUtil.getSize(pageParam);
//                List<String> list = pageParam.getSort();
//                if (list != null && list.size() > 0) {
//                    String sort = list.get(0);
//                    results = energyUnitRepository.findByStationId(stationIdVal, page, size, sort);
//                } else {
//                    results = energyUnitRepository.findByStationId(stationIdVal, page, size);
//                }
//            } else {
//                Sort sort = pageParam == null ? null : QueryUtil.getSort(pageParam.getSort());
//                if (sort != null) {
//                    results = energyUnitRepository.findAll(sort);
//                } else {
//                    List<EnergyUnit> list = energyUnitRepository.findByStationId(stationIdVal);
//                    Collections.sort(list);
//                    results = list;
//                }
//            }
//        } else {
//            if (QueryUtil.needPaging(pageParam)) {
//                results = energyUnitRepository.findAll(QueryUtil.getPageRequest(pageParam));
//            } else {
//                Sort sort = pageParam == null ? null : QueryUtil.getSort(pageParam.getSort());
//                if (sort != null) {
//                    results = energyUnitRepository.findAll(sort);
//                } else {
//                    results = energyUnitRepository.findAll();
//                }
//            }
//        }
//        return results;
//    }
//
//    /**
//     * 能量单元绑定设备
//     * @param id 能量单元id
//     * @param deviceIds 设备ID列表
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void bindDevices(Long id, List<Long> deviceIds) {
//        energyUnitRepository.bindDevices(id, deviceIds);
//    }
//
//    /**
//     * 能量单元解绑设备
//     * @param id 能量单元id
//     * @param deviceIds 设备ID列表
//     */
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void unbindDevices(Long id, List<Long> deviceIds) {
//        energyUnitRepository.unbindDevices(id, deviceIds);
//    }
//
//    /**
//     * 根据能量单元和设备类型查找设备
//     * @param id 能量单元ID
//     * @param type 设备类型
//     */
//    @Override
//    public Iterable<ResourceTree> searchDevices(Long id, String type) {
//        Iterable<ResourceTree> results;
//        if (!StringUtil.isEmpty(type)) {
//            results = energyUnitRepository.searchDevices(id, type);
//        } else {
//            results = energyUnitRepository.searchDevices(id);
//        }
//        return results;
//    }
//
//    /**
//     * 根据能量单元代码获得能量单元
//     * @param code 能量单元代码
//     */
//    private Long getIdByCode(Long code) {
//        if (code == null) return null;
//        EnergyUnit energyUnit = energyUnitRepository.findByCode(code);
//        return energyUnit == null ? null : energyUnit.getCode();
//    }
//}
